namespace Digitalmes.Application.Sys.Configs.Commands;

/// <summary>
/// 系统配置项删除命令。
/// </summary>
/// <param name="Id"></param>
public record DeleteConfigCommand(long Id) : IRequest<IResult<bool>>;

internal sealed class DeleteConfigCommandHandler(IRepository<SysConfig> repo) : IRequestHandler<DeleteConfigCommand, IResult<bool>>
{
    public async Task<IResult<bool>> Handle(DeleteConfigCommand request, CancellationToken cancellationToken)
    {
        if (await repo.IsAnyAsync(m => m.Id == request.Id && m.IsFixed, cancellationToken))
        {
            return Result.Failure("配置项为系统内置，不能删除");
        }

        var ok = await repo.DeleteByIdAsync(request.Id, cancellationToken);
        return Result.From(ok);
    }
}
